package com.ruoyi.wzgl.wzfl.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.wzgl.wzfl.domain.Wzfl;
import com.ruoyi.wzgl.wzfl.service.IWzflService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wzgl.wzfl.mapper.WzflMapper;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

/**
 * 物资分类Service业务层处理
 *
 * @author huacai
 * @date 2024-09-14
 */
@Service
public class WzflServiceImpl implements IWzflService
{
    @Autowired
    private WzflMapper wzflMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询物资分类
     *
     * @param wzflId 物资分类主键
     * @return 物资分类
     */
    @Override
    public Wzfl selectWzflByWzflId(Integer wzflId)
    {
        return wzflMapper.selectWzflByWzflId(wzflId);
    }

    /**
     * 查询物资分类列表
     *
     * @param wzfl 物资分类
     * @return 物资分类
     */
    @Override
    public List<Wzfl> selectWzflList(Wzfl wzfl)
    {
        return wzflMapper.selectWzflList(wzfl);
    }

    /**
     * 新增物资分类
     *
     * @param wzfl 物资分类
     * @return 结果
     */
    @Override
    public int insertWzfl(Wzfl wzfl)
    {
        wzfl.setCreateTime(DateUtils.getNowDate());
        return wzflMapper.insertWzfl(wzfl);
    }

    /**
     * 批量新增物资分类
     *
     * @param wzfls 物资分类List
     * @return 结果
     */
    @Override
    public int batchInsertWzfl(List<Wzfl> wzfls)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(wzfls)) {
            try {
                for (int i = 0; i < wzfls.size(); i++) {
                    int row = wzflMapper.insertWzfl(wzfls.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == wzfls.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改物资分类
     *
     * @param wzfl 物资分类
     * @return 结果
     */
    @Override
    public int updateWzfl(Wzfl wzfl)
    {
        wzfl.setUpdateTime(DateUtils.getNowDate());
        return wzflMapper.updateWzfl(wzfl);
    }

    /**
     * 批量删除物资分类
     *
     * @param wzflIds 需要删除的物资分类主键
     * @return 结果
     */
    @Override
    public int deleteWzflByWzflIds(Integer[] wzflIds)
    {
        return wzflMapper.deleteWzflByWzflIds(wzflIds);
    }

    /**
     * 删除物资分类信息
     *
     * @param wzflId 物资分类主键
     * @return 结果
     */
    @Override
    public int deleteWzflByWzflId(Integer wzflId)
    {
        return wzflMapper.deleteWzflByWzflId(wzflId);
    }

    @Override
    public List<Wzfl> getWzflTree() {
        return wzflMapper.getWzflTree();
    }
}
